package com.ndbg.demo.exception;

import com.ndbg.demo.exception.base.AbstractCustomException;

public class BusinessException extends AbstractCustomException {
    private static final long serialVersionUID = 1L;
    private final int exceptionCode;
    private final Enum<? extends FailureCause> failureCause;


    @Override
    public int getExceptionCode() {
        return exceptionCode;
    }
    /**
     * 获取枚举消息
     *
     * @param enums 枚举类
     * @return message
     */
    protected static String getMessage(Enum<? extends FailureCause> enums) {
        FailureCause failureCause = (FailureCause) enums;
        return failureCause.getMessage();
    }

    public BusinessException(Enum<? extends FailureCause> failureCause, String message) {
        super(message);
        this.exceptionCode = getCode(failureCause);
        this.failureCause = failureCause;
    }

    /**
     * 不可替换的异常信息
     *
     * @param enums ? extends EnumsInterface
     * @param exceptionCode
     */
    public BusinessException(Enum<? extends FailureCause> enums, Throwable cause, int exceptionCode) {
        super(getMessage(enums), cause);
        this.failureCause = enums;
        this.exceptionCode = exceptionCode;
    }

    /**
     * 获取异常枚举
     *
     * @return 异常枚举
     */
    @Override
    public <T extends Enum<T> & FailureCause> T getFailureCause() {
        return (T) failureCause;
    }
}
